<?php
namespace scenic\controllers;
require(__DIR__.'/../excel/PHPExcel.php');
require(__DIR__.'/../excel/PHPExcel/Writer/Excel2007.php');


use Yii;
use yii\web\Controller;
use scenic\models\ScenicDetail;
use scenic\models\ScenicRole;

header("Content-Type:text/html;charset=UTF-8");

class ExporController extends Controller {

	public function init(){
		$view = Yii::$app->view;
		$view->params['layout_data'] = '导出景区';
	}

	/*
	* 根据时间 导出 使用详情到execl
	* @param $start_time 开始时间
	* @param $end_time 结束时间
	* @device 48cc67af070f85aa5535f4c6d36b59e3 945d07b2da959bd28e4f69a999f7ca2f 为测试码 暂时排除掉
	*/

	public function actionIndex(){

		$scenicid = !empty($_GET['scenicid']) ? $_GET['scenicid'] : '';
		//总抽成
		$total = !empty($_GET['total']) ? $_GET['total'] : '';
		$rake = !empty($_GET['rake']) ? $_GET['rake'] : '';
		$sname = $_GET['sname'];

		//景区列表
		if(!empty($_GET['scenicid'])){
			$re = ScenicDetail::find()->where([
				'AND',
				"`scenic_id`= $scenicid",
				"`device`!='48cc67af070f85aa5535f4c6d36b59e3'",
				"`device`!='945d07b2da959bd28e4f69a999f7ca2f'",
			])->all();
		}else {
			$re = ScenicDetail::find()->where([
				'AND',
				"`device`!='48cc67af070f85aa5535f4c6d36b59e3'",
				"`device`!='945d07b2da959bd28e4f69a999f7ca2f'",
			])->all();
		}

		$count = count($re);

		$objexcel = new \PHPExcel();
		$objexcel->getProperties()->setTitle('黑晶去景区管理');
		$objexcel->setActiveSheetIndex(0);

		$objexcel->getActiveSheet()->mergeCells('A1:I1')->mergeCells('B2:F2')->mergeCells('H2:I2');
		$objexcel->getActiveSheet()->setCellValue('A1','黑晶景区设备使用详情')
								->setCellValue('A2','景区名称')
								->setCellValue('B2','设备编码')
								->setCellValue('G2','抽成(元)')
								->setCellValue('H2','创建时间');
		$i=3;
		if($sname == "请选择景区"){
			$scenic = ScenicRole::find()->all();
			foreach($re as $k=>$v){
				$scenicid = $v['scenic_id']-1;
				$v['scenic_id'] = $scenic[$scenicid]['scenic_name'];
				$v['rake'] = $scenic[$scenicid]['rake'];
				$v['create_time'] = date('Y-m-d;H:i:s',$v['create_time']);


				$objexcel->getActiveSheet()->mergeCells('B'.$i.':F'.$i);
				$objexcel->getActiveSheet()->setCellValue('A'.$i,$v['scenic_id'])
								->setCellValue('B'.$i,$v['device'])
								->setCellValue('G'.$i,$v['rake'])
								->setCellValue('H'.$i,$v['create_time']);
				$i++;
				if($i == $count+3) {
					$objexcel->getActiveSheet()->mergeCells('B'.$i.':I'.$i);
					$objexcel->getActiveSheet()->setCellValue('A'.$i,'总抽成')->setCellValue('B'.$i,$total.'元');
				}
			}
		} else {
			foreach($re as $k=>$v){
				$v['create_time'] = date('Y-m-d;H:i:s',$v['create_time']);

				$objexcel->getActiveSheet()->mergeCells('B'.$i.':F'.$i);
				$objexcel->getActiveSheet()->setCellValue('A'.$i,$sname)
								->setCellValue('B'.$i,$v['device'])
								->setCellValue('G'.$i,$rake)
								->setCellValue('H'.$i,$v['create_time']);
				$i++;
				if($i == $count+3) {
					$objexcel->getActiveSheet()->mergeCells('B'.$i.':I'.$i);
					$objexcel->getActiveSheet()->setCellValue('A'.$i,'总抽成')->setCellValue('B'.$i,$count*$rake.'元');
				}
			}
		}



		$objexcel->getActiveSheet()->getStyle('A1:H2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
				//直接输出浏览器
		$objWriter = new \PHPExcel_Writer_Excel5($objexcel);		
		header("Pragma: public");
		header("Expires: 0");
		header("Cache-Control:must-revalidate; post-check=0, pre-check=0");
		header("Content-Type:application/force-download");
		header("Content-Type:application/vnd.ms-execl");
		header("Content-Type:application/octet-stream");
		header("Content-Type:application/download");;
		header('Content-Disposition:attachment;filename="resume.xls"');
		header("Content-Transfer-Encoding:binary");
		return $objWriter->save('php://output');		
	}


}
